#include <math.h>
#include "Vector.h"

Vector::Vector(double x,double y)
{
  this->x = x;
  this->y = y;
}

Vector::Vector(Position* pos)
{
  this->x = pos->x();
  this->y = pos->y();
}

Vector Vector::operator+(const Vector& other) const
{
  Vector res(x+other.x,y+other.y);
  return res;
}

Vector Vector::operator-(const Vector& other) const
{
  Vector res(x-other.x,y-other.y);
  return res;
}

bool Vector::operator==(const Vector& other) const
{
  return (x == other.x && y == other.y);
}

void Vector::scale(double scalar)
{
  x *= scalar;
  y *= scalar;
}

void Vector::normalize()
{
  double len = length();
  if (len == 0) return; // Don't divide by 0
  x = x/len;
  y = y/len;
}

double Vector::lengthSq()
{
  return (x*x)+(y*y);
}

double Vector::length()
{
  return sqrt(lengthSq());
}
